package com.zx.user.web;

import cn.hutool.core.date.DateUtil;
import com.szh.zh.feign.AccountClient;
import com.szh.zh.vo.DateVo;
import com.zx.user.entity.Storage;
import com.zx.user.service.StorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("storage")
@Slf4j
@RefreshScope
public class StorageController {
    @Resource
    private  StorageService storageService;

    @Resource
    private AccountClient accountFeignApi;

    @Resource
    private ContextRefresher contextRefresher;

    @Value("${szh.name}")
    private String name;
    
    @GetMapping("/name")
    public String Name(){
        System.out.println(name);
        return name;
    }
    /**
     * 扣减库存
     * @param code 商品编号
     * @param count 要扣减的数量
     * @return 无
     */
    @PutMapping("/{code}/{count}")
    public ResponseEntity<Void> deduct(@PathVariable("code") String code,@PathVariable("count") Integer count){
        storageService.deduct(code, count);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/list")
    public List<Storage> getList(){
        List<Storage> list = storageService.list();
        return list;
    }
    
    @GetMapping("/testBody")
    public void test(){

        DateVo dateVo = accountFeignApi.testGetRequestBody(Arrays.asList(111L, 222L));
        log.error("ddd->{}",dateVo);
        
    }
    @GetMapping("/java")
    public void test22(){
        Date date = new Date();
        String s = accountFeignApi.testJavaDate(date, "dd");
    }

    @GetMapping("/hujava")
    public void hujava(){
        // 此date 实际类型是 hutool的子类型 DateTime ，在经过Feign调用过程中将抛出异常
        Date date = DateUtil.date();
        String s = accountFeignApi.testHuToolDate(date, "dd");
    }
    
    @GetMapping("/nacos")
    public String nacosDyValue(){
        return storageService.nacosDyValue();
    }
    
    @GetMapping("/re")
    public String ref(){
        new Thread(()->contextRefresher.refresh()).start();
        return "ok";
    }
}
